package com.wuzf.navigation_test.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.wuzf.navigation_test.R;
import com.wuzf.navigation_test.utils.PxUtil;

public class MyPlayerView extends FrameLayout {

    private MyImageView coverView, blurView;
    private ImageView playView;

    public MyPlayerView(@NonNull Context context) {
        this(context, null);
    }

    public MyPlayerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyPlayerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public MyPlayerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        LayoutInflater.from(context).inflate(R.layout.layout_video_player, this, true);
        coverView = this.findViewById(R.id.cover_image);
        blurView = this.findViewById(R.id.blur_image);
        playView = this.findViewById(R.id.video_play);

    }

    public void bindData(int widthPx, int heightPx, int marginLeft, String coverUrl) {
        // 设置cover、blur、play的大小
        int coverWidth, coverHeight;
        int blurWidth, blurHeight;
        int maxWidth = PxUtil.getScreenWidth();
        int maxHeight = PxUtil.getScreenHeight();
        if (widthPx > heightPx) {
            coverWidth = blurWidth = maxWidth;
            coverHeight = blurHeight = (int) (heightPx / (widthPx * 1.0 / maxWidth));
            marginLeft = 0;
        } else {
            coverWidth = maxWidth;
            blurWidth = (int) (widthPx / (heightPx * 1.0 / maxHeight));
            coverHeight = blurHeight = heightPx;
            marginLeft = PxUtil.dp2px(marginLeft);
        }

        if (widthPx > heightPx) {
            blurView.setVisibility(INVISIBLE);
        } else {
            blurView.setBlurImageUrl(blurView, coverUrl, 10);
            blurView.setVisibility(VISIBLE);
        }

        coverView.imageDetail(coverView, coverUrl);

        ViewGroup.LayoutParams blurParams = blurView.getLayoutParams();
        blurParams.height = blurHeight;
        blurParams.width = blurWidth;
        blurView.setLayoutParams(blurParams);

        FrameLayout.LayoutParams coverParams = (LayoutParams) coverView.getLayoutParams();
        coverParams.height = coverHeight;
        coverParams.width = coverWidth;
        coverParams.gravity = Gravity.CENTER;
        coverView.setLayoutParams(coverParams);

        FrameLayout.LayoutParams playParams = (LayoutParams) playView.getLayoutParams();
        playParams.gravity = Gravity.CENTER;
        playView.setLayoutParams(playParams);
    }
}





